//
//  YWHengVideoListController.m
//  videoTest3
//
//  Created by jun peng on 2022/6/19.
//

#import "YWHengVideoListController.h"
#import "YWHomeRecommendVedioCell.h"
#import "YWGetNewsListParam.h"
#import "YWGetNewsListStatus.h"
#import "YWGetNewsResultStatus.h"
#import "YWGetNewsDataStatus.h"
#import "YWGetNewsTool3.h"
#import "YWListCellDelegate.h"
#import "YWNoContentView2.h"


#import "YWHengVideoDetailController.h"

@interface YWHengVideoListController ()<UITableViewDataSource, UITableViewDelegate,YWListCellDelegate,YWNoContentView2Delegate>

@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);

@property(nonatomic,strong)NSMutableArray *statusArray;

@property (nonatomic,assign)NSInteger page;

@property (nonatomic,strong)YWMeStatus *meStatus;
@property(nonatomic,strong)YWNoContentView2 *noContentView;


@end

@implementation YWHengVideoListController



-(YWNoContentView2 *)noContentView{
    
    if (_noContentView == nil) {
        _noContentView = [YWNoContentView2 noContentView];
        _noContentView.delegate = self;
        _noContentView.noContentType = YWNoContentView2Type_NoConcernContent;
    }
    return _noContentView;
}


//懒加载meStatus
-(YWMeStatus *)meStatus{
    //从本地加载meStatus
    _meStatus = [NSKeyedUnarchiver unarchiveObjectWithFile:YWMepath];
    
    return _meStatus;
    
}

-(NSMutableArray *)statusArray{
    if (_statusArray == nil) {
        _statusArray = [NSMutableArray array];
    }
    return _statusArray;
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        //需要在初始化器里面初始化列表视图
        self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    }
    return self;
}


- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];

    self.tableView.frame = self.view.bounds;
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
       self.tableView.dataSource = self;
       self.tableView.delegate = self;
       [self.view addSubview:self.tableView];
    
       self.tableView.tableFooterView = [UIView new];
       self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
       self.tableView.showsVerticalScrollIndicator = NO;
    
       [self loadNewsData];
    
    
}

-(void)loadNewsData{
    
    self.page = 1;
    YWGetNewsListParam *param = [[YWGetNewsListParam alloc] init];
    param.pagesize = @"15";
    param.currentpage = [NSString stringWithFormat:@"%ld",(long)self.page];
    
    param.type = [NSString stringWithFormat:@"%ld",(long)self.status.remarks];
    NSString *userID = self.meStatus.userStatus.Id;
      if (userID != nil) {
           param.userid = userID;
       }
       else{
           
           param.userid = @"";
       }
    
    [YWGetNewsTool3 getNewsListStatusesWithParameters:param success:^(YWGetNewsListStatus * _Nonnull status) {
        
              [self.noContentView removeFromSuperview];
        
               if (status.IsSuccess.integerValue == 1) {
                   
                   self.statusArray = [NSMutableArray arrayWithArray:status.ResultObj.PageInfo];
                   
                   [self.tableView reloadData];
                   
                   //预加载
                   NSMutableArray *urlArray = [NSMutableArray array];
                   for (YWGetNewsDataStatus *dataStatus in status.ResultObj.PageInfo) {
                       [urlArray addObject:dataStatus.FileUrl];
                   }
                   YWVideoPlayerManager *playerManager = [YWVideoPlayerManager sharevideoPlayerManager];
                   [playerManager preLoadUrlArray:urlArray];
                   
                   
                   //假如少于就不需要添加上拉控件了
                  if (status.ResultObj.PageInfo.count == param.pagesize.integerValue) {
                     MJWeakSelf
                      //添加上拉刷新控件
                     self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{

                         [weakSelf loadMoreData];
                     }];
                      
                   }
                   
               }
               else{
                   
                   self.statusArray = [NSMutableArray array];
                   
                   [self.tableView reloadData];
                   
                   [self.view addSubview:self.noContentView];
                   
                   self.noContentView.frame = CGRectMake(0, 0, Wi, self.view.height);
                   
                   self.noContentView.noContentType = YWNoContentView2Type_NoContent;
                   
                   
                   
               }
    } failure:^(NSError * _Nonnull error) {
        
        [self.view addSubview:self.noContentView];
        
        self.noContentView.frame = CGRectMake(0, 0, Wi, self.view.height);
        
        self.noContentView.noContentType = YWNoContentView2Type_Error404;
    }];
    
    
}


-(void)noContentView2DidClickRefreshBtn:(YWNoContentView2 *)noContentView2{
    
    [self loadNewsData];
   
    
}

//加载更多
-(void)loadMoreData{
    
    self.page ++;
    YWGetNewsListParam *param = [[YWGetNewsListParam alloc] init];
    param.pagesize = @"15";
    param.currentpage = [NSString stringWithFormat:@"%ld",(long)self.page];
    
    param.type = [NSString stringWithFormat:@"%ld",(long)self.status.remarks];
    NSString *userID = self.meStatus.userStatus.Id;
      if (userID != nil) {
           param.userid = userID;
       }
       else{
           
           param.userid = @"";
       }
    [YWGetNewsTool3 getNewsListStatusesWithParameters:param success:^(YWGetNewsListStatus * _Nonnull status) {
        
        //移除上拉
        [self.tableView.mj_footer endRefreshing];
        if (status.IsSuccess.integerValue == 1) {
            
            [self.statusArray addObjectsFromArray:status.ResultObj.PageInfo];
            
            [self.tableView reloadData];
            
            //预加载
            NSMutableArray *urlArray = [NSMutableArray array];
            for (YWGetNewsDataStatus *dataStatus in status.ResultObj.PageInfo) {
                [urlArray addObject:dataStatus.FileUrl];
            }
            YWVideoPlayerManager *playerManager = [YWVideoPlayerManager sharevideoPlayerManager];
            [playerManager preLoadUrlArray:urlArray];
            
        }
        else{
            
            //它做的接口没数据是走这里的、所以可以直接移除footer
            [self.tableView.mj_footer removeFromSuperview];
            [MBProgressHUD showError:status.Message];
            
        }
        
    } failure:^(NSError * _Nonnull error) {
        
        //移除上拉
        [self.tableView.mj_footer endRefreshing];
        [MBProgressHUD showError:@"服务器繁忙，请稍后再试"];
    }];
    
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //跳去下一页
    
    YWGetNewsDataStatus *status = self.statusArray[indexPath.row];
    
    YWHomeRecommendVedioCell *cell = [self.tableView cellForRowAtIndexPath:indexPath];
    
    if ([self.delegate respondsToSelector:@selector(hengCellDidTap:cell:withStatus:videoStatusType:vedioIndex:)]) {
        [self.delegate hengCellDidTap:self cell:cell withStatus:status videoStatusType:self.videoStatusType vedioIndex:indexPath.row];
    }
    
}


-(void)listCell:(id)listCell didClickContentWithDataStatus:(YWGetNewsDataStatus *)status{
    
    //跳去下一页
    YWHomeRecommendVedioCell *cell = (YWHomeRecommendVedioCell *)listCell;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    if ([self.delegate respondsToSelector:@selector(hengCellDidTap:cell:withStatus:videoStatusType:vedioIndex:)]) {
        [self.delegate hengCellDidTap:self cell:cell withStatus:status videoStatusType:self.videoStatusType vedioIndex:indexPath.row];
    }
    
}


-(void)listCell:(id)listCell didClickPlayVideoWithDataStatus:(YWGetNewsDataStatus *)status{
    
    //这里是播放
    
    YWHomeRecommendVedioCell *cell = (YWHomeRecommendVedioCell *)listCell;
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    if ([self.delegate respondsToSelector:@selector(hengVideoViewInListVCDidTap:cell:withStatus:videoStatusType:vedioIndex:)]) {
        [self.delegate hengVideoViewInListVCDidTap:self cell:cell withStatus:status videoStatusType:self.videoStatusType vedioIndex:indexPath.row];
    }
    
    

}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.statusArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    YWGetNewsDataStatus *status = self.statusArray[indexPath.row];
    if (status.cellType == YWCellTypeVideo){
        
        YWHomeRecommendVedioCell *cell = [YWHomeRecommendVedioCell cellWithTableView:tableView];
        
        cell.status = status;
        cell.delegate = self;
        
         return cell;
        
    }
    else{
        return nil;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  
    tableView.rowHeight = UITableViewAutomaticDimension;

    tableView.estimatedRowHeight = 416;

    return tableView.rowHeight;
    
}


-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    
    if ([self.delegate respondsToSelector:@selector(hengVideoListController:scrollViewDidEndDragging:willDecelerate:)]) {
        [self.delegate hengVideoListController:self scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if ([self.delegate respondsToSelector:@selector(hengVideoListController:scrollViewDidEndDecelerating:)]) {
        [self.delegate hengVideoListController:self scrollViewDidEndDecelerating:scrollView];
    }
}


-(void)scrollToIndexRow:(NSInteger)index{
    
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
    
}

-(void)scrollToIndexRowToTop:(NSInteger)index{
    
    [self.tableView scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:index inSection:0] atScrollPosition:UITableViewScrollPositionTop animated:YES];
    
}


#pragma mark - JXPagingViewListViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    !self.scrollCallback ?: self.scrollCallback(scrollView);
    
    if ([self.delegate respondsToSelector:@selector(hengVideoListController:scrollViewDidScroll:)]) {
        [self.delegate hengVideoListController:self scrollViewDidScroll:scrollView];
    }
}

- (UIView *)listView {
    return self.view;
}

- (UIScrollView *)listScrollView {
    return self.tableView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
    self.scrollCallback = callback;
}

- (void)listWillAppear {
//    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

- (void)listDidAppear {
//    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

- (void)listWillDisappear {
//    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}

- (void)listDidDisappear {
//    NSLog(@"%@:%@", self.title, NSStringFromSelector(_cmd));
}



@end
